package com.hdc.walnut.controller;

import com.hdc.walnut.common.utils.CRUD.DataDelete;
import com.hdc.walnut.common.utils.CRUD.DataQuery;
import com.hdc.walnut.common.utils.CRUD.DataSave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

import static com.hdc.walnut.common.utils.JSONUtil.*;

@RestController
public class CURDController {

    @Autowired
    DataQuery dataQuery;
    @Autowired
    DataDelete dataDelete;
    @Autowired
    DataSave dataSave;
    // 测试类
    @RequestMapping("/CURDtest")
    public void test(@RequestParam("dataSet") String dataSet) throws IOException {
        System.out.println(dataSet);
    }
    // 查询
    @GetMapping("/get/{code}")
    public ArrayList getToGrid(@PathVariable("code") String code,HttpServletRequest request) {
        try {
            String jsonBody = ReadJsonByCode(ReadJson("walnut.json"), code);
            String tabName = getJsonValueBykey(jsonBody, "tabName");
            System.out.println(request.getQueryString());
            String titles=request.getQueryString()==null?"":URLMatch(getQueryMap(request, "UTF-8"),getJsonValueBykey(jsonBody, "titles"));
            return dataQuery.tableQuery(tabName,titles);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    // 删除
    @DeleteMapping("/del/{code}")
    public Map dataDelete(@PathVariable("code") String code,HttpServletRequest request) {
        try {
            String jsonBody = ReadJsonByCode(ReadJson("walnut.json"), code);
            String tabName = getJsonValueBykey(jsonBody, "tabName");
            String titles=request.getQueryString()==null? "" :URLMatch(getQueryMap(request, "UTF-8"),getJsonValueBykey(jsonBody, "titles"));
            return dataDelete.delete(tabName, titles);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    //  表格保存 dataSet={"id":"1",...}
    @PostMapping("/saveTab/{code}")
    public Map saveTable(@PathVariable("code") String code, @RequestParam("dataSet") String data) {
        try {
            String walnut = ReadJsonByCode(ReadJson("walnut.json"), code);
            String tabName = getJsonValueBykey(walnut, "tabName");
            ArrayList<Map<String, Object>> dataSet = toArrayList("["+data+"]");
            System.out.println("[params]:tabName:" + tabName + ";dataSet:" + dataSet);
            return dataSave.tableSave(tabName, dataSet);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    // 表单保存 dataSet={"name":"id","value":"1",...}
    @PostMapping("/saveForm/{code}")
    public Map saveForm(@PathVariable("code") String code, @RequestParam("dataSet") String data) {
        try {
            String walnut = ReadJsonByCode(ReadJson("walnut.json"), code);
            String tabName = getJsonValueBykey(walnut, "tabName");
            ArrayList<Map<String, Object>> dataSet = toArrayList("["+data+"]");
            System.out.println("[params]: tabName:" + tabName + ";dataSet:" + dataSet);
            return dataSave.formSave(tabName, dataSet);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    // 注册 密码MD5加密
    @PostMapping("/userMD5Save")
    public Map<String, Object> userSaveMD5(@PathVariable("code") String code, @RequestParam("dataSet") String data){
        try {
            String walnut = ReadJsonByCode(ReadJson("walnut.json"), code);
            String tabName = getJsonValueBykey(walnut, "tabName");
            ArrayList<Map<String, Object>> dataSet = toArrayList("[" + data + "]");
            System.out.println("[params]: tabName:" + tabName + ";dataSet:" + dataSet);
            for (Map<String, Object> tresult : dataSet) {
                if (tresult.get("name").equals("password")) {
                    String password = String.valueOf(tresult.get("values"));
                    String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
                    tresult.put("values", md5Password);
                }
            }
            return dataSave.tableSave(tabName, dataSet);
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return null;
    }
}